/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 */

package com.device.idea.demo.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.device.idea.demo.constant.Constant;
import com.device.idea.whiteboard.model.WhiteBoardPenStyle;

import java.io.Closeable;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 公共方法类
 *
 * @since 2020-12-24
 */
public class MethodUtil {
    static final String TAG = MethodUtil.class.getSimpleName();

    private static final String PARAM_SUPPORT4K = "support4k";

    private static final int WB_FILE_SIZE_MAX = 1024 * 1024 * 1024; // 文件最大大小定义为1G

    private MethodUtil() {
    }

    /**
     * 判断是否支持4k
     *
     * @param context 上下文
     * @return 是否支持4k
     */
    public static boolean isSupport4k(Context context) {
        ApplicationInfo info = null;
        try {
            info =
                context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException exception) {
            LogUtil.error(TAG, "isSupport4k error");
        }
        if (info == null || info.metaData == null) {
            LogUtil.error(TAG, "isSupport4k ignore: info is null");
            return false;
        }
        return info.metaData.getBoolean(PARAM_SUPPORT4K);
    }

    /**
     * getPenResFromType
     *
     * @param type penType
     * @return pen resource arrays
     */
    public static int[] getPenResFromType(int type) {
        int[] resArray = Constant.PEN_TOP_DRAWABLE_PENCIL;
        WhiteBoardPenStyle penStyle = WhiteBoardPenStyle.getEnum(type);
        switch (penStyle) {
            case NORMAL_PEN:
                resArray = Constant.PEN_TOP_DRAWABLE_PENCIL;
                break;
            case BRUSH_PEN:
                resArray = Constant.PEN_TOP_DRAWABLE_BRUSH;
                break;
            case HIGHLIGHTER_PEN:
                resArray = Constant.PEN_TOP_DRAWABLE_HIGHLIGHTER;
                break;
            case SMART_PEN_TEXT:
                resArray = Constant.PEN_TOP_DRAWABLE_TEXT;
                break;
            case SMART_PEN_GRAPHICS:
                resArray = Constant.PEN_TOP_DRAWABLE_GRAPHICS;
                break;
            default:
                Log.w(TAG, "unSupport penStyle=" + penStyle);
                break;
        }

        return resArray;
    }

    /**
     * 通过uri复制文件到应用目录
     *
     * @param context the context
     * @param data the data
     * @return the file from file provider
     */
    public static String getFileFromFileProvider(Context context, Uri data) {
        if (data == null) {
            return "";
        }

        String newPath = "";
        InputStream is = null;
        FileOutputStream out = null;
        ParcelFileDescriptor pfd = null;
        try {
            File file = new File(context.getCacheDir().getCanonicalPath());
            if (!file.exists() && !file.mkdirs()) {
                LogUtil.error(TAG, "mkdirs error!");
                return "";
            }
            newPath = file.getCanonicalPath() + File.separator + "temp.cwb";
            out = new FileOutputStream(newPath);
            pfd = context.getContentResolver().openFileDescriptor(data, "r");
            if (pfd == null) {
                LogUtil.error(TAG, "ParcelFileDescriptor null!");
                return "";
            }
            FileDescriptor fd = pfd.getFileDescriptor();
            if (fd == null) {
                LogUtil.error(TAG, "fd null!");
                return "";
            }
            is = new FileInputStream(fd);
            if (is.available() > WB_FILE_SIZE_MAX) {
                return "";
            }
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = is.read(buff)) > 0) {
                out.write(buff, 0, len);
            }
        } catch (IOException | SecurityException en) {
            LogUtil.error(TAG, "getFileFromFileProvider read or denial error!");
        } finally {
            closeFd(is);
            closeFd(pfd);
            closeFd(out);
        }
        return newPath;
    }

    /**
     * 关闭资源
     *
     * @param fd the fd
     */
    public static void closeFd(Closeable fd) {
        if (fd != null) {
            try {
                fd.close();
            } catch (IOException e) {
                LogUtil.error(TAG, "closeFd  close error!");
            }
        }
    }
}
